import { reactive, ref } from "vue";
import { getBusinessSelectListApi } from "@/api/insuranceCompany";

export default function InitHook(state: any) {
  const { passData } = state;
  //表单数据
  const formState = reactive<PlaceOrder.editForm>({
    waybillNum: "",
    customerName: "",
    itemNumber: "",
    actualWeight: "",
    declaredValue: "",

    orderCompanyReqList: [
      {
        insuranceCompanyId: "", //保险公司ID
        insuranceAmount: "", //投保金额
        insuranceType: "", //保险类型
        insuranceNature: "", //	保险性质
        insuranceRemark: "" //保险备注
      }
    ]
  });

  const placeOrderCompanyOpts = ref<any>([]); //保险公司

  const tableData = reactive([]); //表格数据

  const loading = ref<boolean>(false); //等待状态

  //获取保险公司
  const getCountryApi = () => {
    return getBusinessSelectListApi(passData.value.waybillId).then((res: any) => {
      const { data } = res;
      placeOrderCompanyOpts.value = data.map((t: any) => {
        return { name: t.name, id: t.id };
      });
    });
  };

  //表单ref
  const formRef = ref(null);
  return {
    tableData,
    formState,
    loading,
    formRef,
    getCountryApi,
    placeOrderCompanyOpts
  };
}
